package com.his.dictrole.service.Imp;

import com.his.common.verification.group.Update;
import com.his.dictrole.dao.DictRoleMapper;
import com.his.dictrole.dao.DictRoleMenuMapper;
import com.his.dictrole.entity.DictRole;
import com.his.dictrole.entity.DictRoleMenu;
import com.his.dictrole.service.DictRoleService;
import com.his.dictrole.vo.DictRoleMenuVo;
import com.his.dictrole.vo.DictRoleVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.WorkIdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaoxiaoping
 * @Date 2020/3/9 13:24
 * @Version 1.0
 */
@Service
public class DictRoleServiceImpl implements DictRoleService {
    @Resource
    private DictRoleMapper dictRoleMapper;
    @Resource
    private DictRoleMenuMapper dictRoleMenuMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DictRoleVo dictRoleVo,DictEmployee dictEmployee){
            DictRole dictRole = BeanUtil.copyBean(dictRoleVo, DictRole.class);
            dictRole.setId(WorkIdUtil.nextId());
            com.xiao.common.entity.DictEmployee dictEmployee1 = BeanUtil.copyBean(dictEmployee, com.xiao.common.entity.DictEmployee.class);
            dictRole.setCreateUser(dictEmployee1);
            dictRoleMapper.insert(dictRole);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id){
        dictRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DictRole> qureyAll(){
        return dictRoleMapper.selectAll();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DictRole selectById(String id){
        return dictRoleMapper.selectByPrimaryKey(id);
    }

    /**
     * 角色绑定
     * @param dictRoleMenuVo
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void roleBindMenu(DictRoleMenuVo dictRoleMenuVo){
        dictRoleMenuMapper.deleteByRole(dictRoleMenuVo.getRoleId());
        String[] menuIds = dictRoleMenuVo.getMenuIds();
        for(String menuId : menuIds){
            DictRoleMenu dictRoleMenu = new DictRoleMenu();
            dictRoleMenu.setId(WorkIdUtil.nextId());
            dictRoleMenu.setRoleId(dictRoleMenuVo.getRoleId());
            dictRoleMenu.setMenuId(menuId);
            dictRoleMenuMapper.insert(dictRoleMenu);
        };
    }

    @Override
    public void updateById(DictEmployee dictEmployee, @Validated({Update.class}) DictRoleVo dictRoleVo) {
        DictRole dictRole = BeanUtil.copyBean(dictRoleVo, DictRole.class);
        com.xiao.common.entity.DictEmployee dictEmployee1 = BeanUtil.copyBean(dictEmployee, com.xiao.common.entity.DictEmployee.class);
        dictRole.setUpdaterUser(dictEmployee1);
        dictRoleMapper.updateByPrimaryKeySelective(dictRole);
    }

    /**
     * 通过roleid查绑定的菜单
     * @param roleId
     * @return
     */
    @Override
    public List<String> selectMenuByRole(String roleId) {
       List<DictRoleMenu> dictRoleMenuList = dictRoleMenuMapper.selectByRole(roleId);
       List<String> menuIds = new ArrayList<>();
       for(DictRoleMenu dictRoleMenu : dictRoleMenuList){
           menuIds.add(dictRoleMenu.getMenuId());
       }
        return menuIds;
    }

    @Override
    public List<DictRole> getShowRole() {
        DictRole dictRole = new DictRole();
        dictRole.setIsShow(1);
        return  dictRoleMapper.selectByEntity(dictRole);
    }
}
